﻿using System.Security;

namespace _08_DialogSystem
{
    internal class DialogSystem
    {
        struct Data
        {
            public int id;
            public bool isOption;
            public string content1;
            public int nextId1;
            public string content2;
            public int nextId2;
        }

        Data[] datas;
        int offsetX, offsetY;
        int row,col;
        long lastTime, currentTime;
        int selected;
        ConsoleKeyInfo key;
        Data currentData;
        bool endFlag;

        public DialogSystem()
        {
            Console.CursorVisible = false;
            LoadData();
            offsetX = 5; 
            offsetY = 20;
            row = 6;
            col = 40;
            currentData = GetData(0);
            endFlag = true;
        }

        // 从文件中读取对话数据并存入数组
        void LoadData()
        {
            string fileName = "../../../DialogData.txt";
            if (File.Exists(fileName))
            {
                // 读取文件, 这里using表示只在这里读取(使用？)，出了之后释放？以免内存泄漏
                using (StreamReader sr = new StreamReader(fileName))
                {
                    string data = sr.ReadToEnd();
                    // 字符串分割 Split, 这里的文本中用",""换行"分离
                    string[] lines = data.Split('\n'); // 分隔的也可使字符串
                    // 根据数据行数 分配内存空间
                    datas = new Data[lines.Length-1];
                    for (int i = 0; i < datas.Length; i++)
                    {
                        string[] elements = lines[i + 1].Split('|');
                        datas[i].id = int.Parse(elements[0]);
                        datas[i].isOption = elements[1] == "true";
                        datas[i].content1 = elements[2];
                        datas[i].nextId1 = int.Parse(elements[3]);
                        if (!datas[i].isOption)continue;
                        datas[i].content2 = elements[4];
                        datas[i].nextId2 = int.Parse(elements[5]);
                    }
                };
            }
        }

        // 根据id获取对话数据
        Data GetData(int id)
        {
            Data data = new Data();

            for (int i = 0;i < datas.Length;i++)
            {
                if (datas[i].id == id)
                {
                    data = datas[i];
                    break;
                }
            }

            return data;
        }

        void Clear()
        {
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                {
                    
                    MyPrint("  ", offsetX + j, offsetY + i);
                }
            }
        }

        void PrintFrame()
        {
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                {
                    string temp = "";
                    if (i == 0 && j == 0)
                    {
                        /*MyPrint("┌", offsetX + j, offsetY + i);*/
                        temp = "┌";
                    }
                    else if (i == 0 && j == col - 1)
                    {
                        /*MyPrint("┐", offsetX + j, offsetY + i);*/
                        temp = "┐";
                    }
                    else if (i == row -1  && j == 0)
                    {
                        /*MyPrint("└", offsetX + j, offsetY + i);*/
                        temp = "└";
                    }
                    else if (i == row - 1 && j == col - 1)
                    {
                        /*MyPrint("┘", offsetX + j, offsetY + i);*/
                        temp = "┘";
                    }
                    else if (i == 0 || i == row - 1)
                    {
                        /* MyPrint("一", offsetX + j, offsetY + i);*/
                        temp = "─";
                    }
                    else if (j == 0 || j == col - 1)
                    {
                        /* MyPrint("|", offsetX + j, offsetY + i);*/
                        temp = "│";
                    }
                    //else
                    //{
                    //    temp = "  ";
                    //}
                    MyPrint(temp, offsetX + j, offsetY + i);
                }
            }
        }

        void PrintText()
        {
            if (!currentData.isOption)
            {
                string str = currentData.content1;
                Console.SetCursorPosition((offsetX + 2) * 2, offsetY + 2);
                for (int i = 0; i < str.Length; i++)
                {

                    if (i == 36)
                    {
                        Console.SetCursorPosition((offsetX + 2) * 2, offsetY + 3);
                    }
                    Console.Write(str[i].ToString());
                    Thread.Sleep(10);
                }
            }
            else
            {
                string str = currentData.content1;
                Console.SetCursorPosition((offsetX + 2) * 2, offsetY + 2);
                Console.ForegroundColor = selected == 0 ? ConsoleColor.Red : ConsoleColor.White; 
                for (int i = 0; i < str.Length; i++)
                {
                    Console.Write(str[i].ToString());
                    Thread.Sleep(10);
                }
                str = currentData.content2;
                Console.SetCursorPosition((offsetX + 2) * 2, offsetY + 3);
                Console.ForegroundColor = selected == 1 ? ConsoleColor.Red : ConsoleColor.White;
                for (int i = 0; i < str.Length; i++)
                {
                    Console.Write(str[i].ToString());
                    Thread.Sleep(10);
                }
                Console.ResetColor();
            }
            
        }

        public void Render()
        {
            if(endFlag)
            {
                return;
            }
            PrintFrame();
            PrintText();
        }

        

        void MyPrint(string str, int x, int y, ConsoleColor color = ConsoleColor.White)
        {
            Console.SetCursorPosition(x * 2, y);
            Console.ForegroundColor = color;
            Console.Write(str);
            Console.ResetColor();
        }

        public void StartDialog(int id)
        {
            currentData = GetData(id);
            endFlag = false;
        }

        public void Input()
        {
            key = default;
            if (Console.KeyAvailable)
            {
                key = Console.ReadKey(true);
            }

            switch (key.Key)
            {
                case ConsoleKey.W:
                    if (currentData.isOption)
                    {
                        selected--;
                    }
                    if(selected < 0)
                    {
                        selected = 1;
                    }
                    break;
                case ConsoleKey.S:
                    if (currentData.isOption)
                    {
                        selected++;
                    }
                    if(selected > 1)
                    {
                        selected = 0;
                    }
                    break;
                case ConsoleKey.Enter:
                    int nextId = 0;
                    if (currentData.isOption)
                    {
                        nextId = selected == 0? currentData.nextId1 : currentData.nextId2;
                    }
                    else
                    {
                        nextId = currentData.nextId1;
                    }
                    if (nextId == -1)
                    {
                        endFlag = true;
                    }
                    currentData = GetData(nextId);
                    selected = 0;
                    Clear();
                    break;
            }
        }
    }
}
